//
//  MoreViewController.m
//  Discovery
//
//  Created by HappyMilk 02 on 2017/7/6.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "MoreViewController.h"
#import "MoreMsgViewController.h"
#import "StatementViewController.h"
#import "MoreViewCell.h"
#import "HeadViewCell.h"
#import "MJUserModel.h"
#import "LoginHeader.h"
#import "UIImageView+WebCache.h"
#import "MJMyMsgController.h"
#import "LoginViewController.h"

@interface MoreViewController ()<UITableViewDelegate ,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tabelview;

@property (nonatomic ,strong) MJUserModel *userModel;

@end

@implementation MoreViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [MJUserModel registerSubclass];
    if ([MPWeChat stander].m_isLogin) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"first"]) {
            
            [self findUserData];
        }else{
            
            [self createUserModel];
        }
    }
}
- (void)findUserData{
    
    AVQuery *query = [AVQuery queryWithClassName:@"User_list"];
    NSString *objcid = [[NSUserDefaults standardUserDefaults] objectForKey:@"first"];
    [query getObjectInBackgroundWithId:objcid block:^(AVObject * _Nullable object, NSError * _Nullable error) {
        MJUserModel *model = (MJUserModel *)object;
        _userModel = model;
        [self.tabelview reloadData];
    }];
}
- (void)createUserModel{
    
    MJUserModel *model = [MJUserModel object];
    NSLog(@"------ %@",model.objectId);
    model.user_id = @"";
    model.name = NSLocalizedStringFromTable(@"Tomi", @"morePlist", nil);
    model.phoneNum = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    model.sex = NSLocalizedStringFromTable(@"man", @"morePlist", nil);
    model.adrs = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    model.adrs_now = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    model.birthday = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    model.height = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    model.school_record = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    model.enmotion = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    model.signature = NSLocalizedStringFromTable(@"nol", @"morePlist", nil);
    UIImage *image = [UIImage imageNamed:@"user_head_df"];
    NSData *imageData=UIImageJPEGRepresentation(image,0.6);
    AVFile *file = [AVFile fileWithName:@"more_head_df.png" data:imageData];
    model.head_img = file;
    [model saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        [[NSUserDefaults standardUserDefaults] setObject:model.objectId forKey:@"first"];

    }];
    _userModel = model;
    [self.tabelview reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    
    
    
    
    UIView *view = [[UIView alloc] init];
    self.tabelview.tableFooterView = view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
            break;
        default:
            return 4;
            break;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        HeadViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headcell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HeadViewCell" owner:nil options:nil] lastObject];
        }
        [cell.imgv_head sd_setImageWithURL:[NSURL URLWithString:_userModel.head_img.url] placeholderImage:[UIImage imageNamed:@"more_head_df.png"]];
        if ([MPWeChat stander].m_isLogin) {
            cell.lab_username.text = _userModel.name;
        }else
            cell.lab_username.text = NSLocalizedStringFromTable(@"Login...", @"morePlist", nil);
        return cell;
    }else{
        MoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"morecell"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MoreViewCell" owner:nil options:nil] lastObject];
        }
        [cell createCellBy:indexPath];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 80;
    }else
        return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 30;
    }else
        return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if ([MPWeChat stander].m_isLogin) {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mine" bundle:nil];
            MJMyMsgController *vc = [sb instantiateViewControllerWithIdentifier:@"MJMyMsgController"];
            vc.userModel = _userModel;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"login" bundle:nil];
            LoginViewController *vc = [sb instantiateViewControllerWithIdentifier:@"LoginViewController"];
            [self presentViewController:vc animated:YES completion:nil];
        }
    }else{
        if (indexPath.row == 0) {
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"more" bundle:nil];
            MoreMsgViewController *vc = [sb instantiateViewControllerWithIdentifier:@"MoreMsgViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1){
            [[UIPasteboard generalPasteboard] setString:@"759120403@qq.com"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:759120403@qq.com"]];
        }else if (indexPath.row == 2){
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"more" bundle:nil];
            StatementViewController *vc = [sb instantiateViewControllerWithIdentifier:@"StatementViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [[UIPasteboard generalPasteboard] setString:@"759120403@qq.com"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:759120403@qq.com"]];
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
